#include "time_manager.h"

extern __IO uint32_t u32SystemTick100us;
uint32_t adc1_times = 0, adc_0_5s_times = 0, time_too_long_cnt = 0, time_too_long_0_5s_times = 0;
__IO uint16_t u16SystemTime100us;
__IO uint16_t u16SystemTime200us;
__IO uint16_t u16SystemTime1ms;
__IO uint16_t u16SystemTime2ms;
__IO uint16_t u16SystemTime10ms;
__IO uint16_t u16SystemTime50ms;
__IO uint16_t u16SystemTime100ms;
__IO uint16_t u16SystemTime500ms;
extern pole_status pole_status_set;
extern uint32_t u16ADC1_16in1_adc[10];
extern uint32_t u16ADC1_4in1_adc[10];
//设置转速
char motor_run_mode = 0, get_status_time = 0, calculate_PID_now = 0, write_in_calculate_result = 0, calculate_current_PID_now = 0, check_current_per_circle;
//设置pid控制指针
extern pid_virables pid_voltage_error_controller, pid_current_error_controller, pid_time_set_controller;
extern char calculated_speed_control;
//设置和引用误差计算
int16_t calculate_PID_output_by_time_error = 0, calculate_pid_output_by_voltage_error = 0, nagetive_feedback_by_time_error = 0;
extern u16 reserving_time_after_correction, half_limit_reserving_time, condition_judgement_calculated, duty_calculated_from_current;
extern int16_t error_sum, crossing_time_error_sum;
int16_t change_PID_counter = 0, change_PID_flag = 0;
extern int error_sum_of_current;
//引用均值计算
extern char reading_average_now;
//设置均值计算循环数值
extern char i_average_test;

uint16_t min_circle_time = 1000, get_error_flag = 0, get_error_count = 0;
int32_t get_error_after_min_time[64];
// 0、停止 1、中间状态2、静态计算模式 3、开环加速 4、闭环调压 5、闭环调速
uint32_t speed_run_real = 0;
uint32_t i = 0;
void System_Time_Management(void)
{
    //    TIM_BDTRInitTypeDef TIM_BDTRInitStruct; // 20201027

    //    uint16_t u16TempBDTRStatusRead; // 20201027
    if (u32SystemTick100us > 0) // 100us loop, 100us executed once in this loop
    {
        TIM1->CCR4 = 0;
        TIM1->CCR5 = 0;
        u32SystemTick100us--;
        u16SystemTime100us++;
        if (calculate_current_PID_now)
        {
            calculate_current_PID_now = 0;
            // duty_calculated_from_current = calculate_pid_output(&pid_current_error_controller, error_sum_of_current);
            write_in_calculate_result = 1;
        }
    }
    if (u16SystemTime100us > 2) // 200us loop, 200us executed once in this loop
    {

        u16SystemTime100us -= 2;
        u16SystemTime200us++;
    }
    if (u16SystemTime200us > 5) // 1ms loop, 1ms executed once in this loop
    {
        if ((!calculated_speed_control) && calculate_PID_now)
        {

            check_certain_stage_error_current();
            if (change_PID_flag)
            {
                crossing_time_error_sum = 0;
                check_rise_down_error_voltage();
                if (calculate_PID_output_by_time_error < 200)
                {
                    change_PID_flag = 0;
                    change_PID_counter = 0;
                }
            }
            else
            {
                all_times_error_sum();
                error_sum = -2000; //
                if (calculate_PID_output_by_time_error > 200 && change_PID_counter < 30)
                {
                    change_PID_counter++;
                    change_PID_flag = 0;
                }
                else if (calculate_PID_output_by_time_error < 200)
                {
                    change_PID_flag = 0;
                    change_PID_counter = 0;
                }
                else
                {
                    change_PID_flag = 1;
                }
            }
            calculate_PID_output_by_time_error = calculate_pid_output(&pid_time_set_controller, crossing_time_error_sum);
            calculate_pid_output_by_voltage_error = calculate_pid_output(&pid_voltage_error_controller, error_sum);
            nagetive_feedback_by_time_error = half_limit_reserving_time - calculate_PID_output_by_time_error;
            reserving_time_after_correction = nagetive_feedback_by_time_error - calculate_pid_output_by_voltage_error;
            if (reserving_time_after_correction < 9)
            {
                reserving_time_after_correction = 9;
            }

            condition_judgement_calculated = reserving_time_after_correction;
            duty_calculated_from_current = calculate_pid_output(&pid_current_error_controller, error_sum_of_current);
            BLDC_motor_modulation_value_set(duty_calculated_from_current);
            calculate_new_check_time();
            if (reserving_time_after_correction < min_circle_time)
            {
                min_circle_time = reserving_time_after_correction;
                get_error_count = 0;
                get_error_flag = 1;
            }
            if (get_error_flag && get_error_count < 64)
            {
                get_error_after_min_time[get_error_count] = error_sum;
                get_error_count++;
            }
            else
            {
                get_error_flag = 0;
                //      get_error_count = 0;
            }

            calculated_speed_control = 6;
            calculate_PID_now = 0;
        }

        u16SystemTime200us -= 5;
        u16SystemTime1ms++;
    }
    else if (u16SystemTime1ms > 2) // 2ms loop, 2ms executed once in this loop
    {

        if (motor_run_mode == 2)
        {
            set_voltage_and_current_average();
            adc_count_clean(); //计数器清零
        }

        u16SystemTime1ms -= 2;
        u16SystemTime2ms++;
    }
    else if (u16SystemTime2ms > 5) // 10ms loop, 10ms executed once in this loop
    {
        u16SystemTime2ms -= 5;
        u16SystemTime10ms++;
    }
    else if (u16SystemTime10ms > 5) // 50ms loop,  50ms executed once in this loop
    {
        u16SystemTime10ms -= 5;
        u16SystemTime50ms++;
    }
    else if (u16SystemTime50ms > 2) // 100ms loop, 100ms executed once in this loop
    {
        //   speed_run_real = get_speed_now();
        u16SystemTime50ms -= 2;
        u16SystemTime100ms++;
    }
    else if (u16SystemTime100ms > 5) // 500ms loop,  500ms executed once in this loop
    {
        u16SystemTime100ms -= 5;
        u16SystemTime500ms++;

        //切换模式
        switch (motor_run_mode)
        {
        case 1:
            set_auto_rise_duty_virable(1, 10, 0, 400); //设置初始化电压
            reading_average_now = 1;
            motor_status_set(2, 2, 2);               //给一半pwm计算中值
            set_condition_judgement_setting(0xffff); //拉长判断值
            BLDC_change_mode_compare();
            if (get_status_time-- <= 0)
            {
                get_status_time = 3;
                motor_run_mode = 2;
            }

            break;
        case 2:

            if (get_status_time-- <= 0)
            {
                motor_run_mode = 3;
                i_average_test = 0;
                // BLDC_motor_modulation_value_set(get_modulation_value() + 100); //
                BLDC_change_mode_compare();
                get_status_time = 3;
            }
            break;
        case 3:
            reading_average_now = 0;
            set_motor_derection(0);                                       //定义正转
            set_condition_judgement_setting(PWM_FREQUENCY / 10 / 12 / 2); //设定初始10转/s，即120次换向
            if (get_status_time-- <= 1)
            {
                motor_run_mode = 4;                         //延迟一定时间后进入闭环调压
                change_to_use_inside_calculate_reserving(); //切换成pid控制的换向定位
                BLDC_motor_modulation_value_set(60);        //提升电压
            }
            break;
        case 4:

            if (get_modulation_value() >= 480)
            {
                motor_run_mode = 5;
            }

            break;
        case 5:
            break;
        default:
            break;
        }
        printf("adc1_times:%d\n", adc1_times);
        time_too_long_0_5s_times = time_too_long_cnt;
        time_too_long_cnt = 0;
        adc_0_5s_times = adc1_times;
        adc1_times = 0;
        LED2_TOGGLE();
    }
    else if (u16SystemTime500ms > 2)
    {
        u16SystemTime500ms -= 4;
        // motor_status_change_auto();
    }
}
void set_motor_start(void)
{
    motor_run_mode = 1;
    get_status_time = 1;
    reading_average_now = 1;
    BLDC_motor_modulation_value_set(100); //初始调制度设计
}
